import Vue from "vue";

// 封装存储cookie的方法
const setCookie = function (name, value, hours) {
  // 创建一个日期对象，用来存储过期时间
  let date = new Date()
  date.setTime(date.getTime() + (hours - 8) * 1000 * 3600)
  document.cookie = name + '=' + value + ';expires=' + date
};

// 封装获取cookie的方法
const getCookie = function (name) {
  let cookies = document.cookie.split(' ')
  for (let i = 0; i < cookies.length; i++) {
    if (RegExp(name).test(cookies[i])) {
      return cookies[i].split('=')[1].replace(';', '')
    }
  }
};

// 封装清除cookie的方法
const removeCookie = function (name) {
  let date = new Date()
  date.setTime(date.getTime() - 12 * 1000 * 3600)
  document.cookie = name + '=' + '' + ';expires=' + date
};

const utils = {
  setCookie,
  getCookie,
  removeCookie
}

Vue.prototype.$utils = utils;

export default utils;
